﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YBApproval.IDAL;
using YBApproval.BLL;
using YBApproval.Model;

namespace YBApproval
{
    public partial class _Default : System.Web.UI.Page
    {
        private string processDisplay = "<tr><td style=\"width:220px;\"><li class=\"ProcessText\"><a href=\"Pages/Request.aspx?IsNew=true&RequestId=#ProcessID#\"  id=\"#APathID1#\">#ProcessName#</a></li></td>"
                            + "<td><a href=\"#ProcessHelpPath#\" id=\"#APathID3#\"><img src=\"/images/help.png\" style=\"cursor:hand;\" /></a></td><td style=\"width:5px;\">&nbsp;</td>"
                            + "<td><a href=\"Pages/Request.aspx?IsNew=true&RequestId=#ProcessID#\" id=\"#APathID2#\"><img src=\"/images/process.png\" style=\"cursor:hand;\" /></a></td><td style=\"width:5px;\">&nbsp;</td>"
                            + "<td style=\"width:40px;\"></td></tr>";
        public string Table1Rows;
        public string Table2Rows;
        protected void Page_Load(object sender, EventArgs e)
        {
            //this.UCView1.RecordId = new Guid("FA22AB8B-5B4C-42FD-BAC6-FED1D08195EE");

            if (YBPSession.Instance.CurrentUser == null)
            {
                Response.Redirect(@"~/Login.aspx");
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
        }

        protected override void OnPreRender(EventArgs e)
        {
            //在该事件发生前：

            //Page 对象会针对每个控件和页调用 EnsureChildControls。 

            //设置了 DataSourceID 属性的每个数据绑定控件会调用 DataBind 方法。有关更多信息，请参见下面的数据绑定控件的数据绑定事件。

            //页上的每个控件都会发生 PreRender 事件。使用该事件对页或其控件的内容进行最后更改。

            base.OnPreRender(e);

            if (!IsPostBack)
            {
                Master.SetContentTitle("我的信息大厅");
                NewProcessDataInit();
                MyGVInit();
            }

        }

        /// <summary>
        /// 启动新流程的数据初始化
        /// </summary>
        private void NewProcessDataInit()
        {
            using (IProcess ProcessDAL = DALFactory.GetDAL<IProcess>())
            {
                IList<Form_ProcessSet> result = ProcessDAL.GetAllProcesses();//使用三层架构实现,调用业务逻辑层的方法
                if (result != null && result.Count > 0)
                {
                    int cou = (result.Count + 1) / 2;
                    for (int i = 0; i < cou; i++)
                    {
                        Table1Rows += RowBuilder(result[i]);
                    }

                    for (int i = cou; i < result.Count; i++)
                    {
                        Table2Rows += RowBuilder(result[i]);
                    }
                }
            }
        }

        private string RowBuilder(Form_ProcessSet p)
        {
            string newrow = processDisplay;
            newrow = newrow.Replace("#ProcessID#", p.Form.Id.ToString());
            newrow = newrow.Replace("#ProcessName#", p.Process.Name);
            newrow = newrow.Replace("#APathID1#", "a1" + p.Process.Id.ToString());
            newrow = newrow.Replace("#APathID2#", "a2" + p.Process.Id.ToString());
            newrow = newrow.Replace("#APathID2#", "a3" + p.Process.Id.ToString());
            return newrow;
        }

        /// <summary>
        /// My current Proccess Data GridView Init
        /// </summary>
        private void MyGVInit()
        {
            using (IInstanceApproval IAD = DALFactory.GetDAL<IInstanceApproval>())
            {
                List<InstanceApproval> MyWork = IAD.GetWorkItems(YBPSession.Instance.CurrentUser.Id);
                if (MyWork != null && MyWork.Count > 0)
                {
                    List<InstantView> MyWorkView = PageDataBandHelper.GetInstantView(MyWork);
                    gvMyCurrentAction.DataSource = MyWorkView;
                    gvMyCurrentAction.DataBind();
                }
                else
                {
                    gvMyCurrentAction.DataSource = null;
                    gvMyCurrentAction.DataBind();
                }


                List<Instance> Drafts = IAD.GetDraftItems(YBPSession.Instance.CurrentUser.Id);
                if (Drafts != null && Drafts.Count > 0)
                {
                    List<InstantView> MyDraftsView = PageDataBandHelper.GetInstantView(Drafts);
                    gvDraft.DataSource = MyDraftsView;
                    gvDraft.DataBind();
                }
                else
                {
                    gvDraft.DataSource = null;
                    gvDraft.DataBind();
                }

                List<InstanceApproval> OpenItems = IAD.GetOpenItems(YBPSession.Instance.CurrentUser.Id);
                if (OpenItems != null && OpenItems.Count > 0)
                {
                    List<InstantView> MyOpenItemsView = PageDataBandHelper.GetInstantView(OpenItems);
                    gvMyInprocess.DataSource = MyOpenItemsView;
                    gvMyInprocess.DataBind();
                }
                else
                {
                    gvMyInprocess.DataSource = null;
                    gvMyInprocess.DataBind();
                }
            }
        }

        protected void gvMyCurrentAction_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Operation")
            {
                Response.Redirect(@"~/Pages/Approval.aspx?RecordId=" + e.CommandArgument.ToString()); 
            }
        }



        protected void gvDraft_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditDraft")
            {
                Response.Redirect(@"~/Pages/Request.aspx?IsNew=false&RequestId=" + e.CommandArgument.ToString()); 
            }
            else if (e.CommandName == "DelDraft")
            {
                using (IInstance IAD = DALFactory.GetDAL<IInstance>())
                {
                    IAD.Delete(e.CommandArgument.ToString());
                }
            }
        }

        protected void gvMyInprocess_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ViewDetail")
            {
                Response.Redirect(@"~/Pages/View.aspx?RecordId=" + e.CommandArgument.ToString());
            }
        }
    }    
}
